home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / picture / trans.c < prev    next >
Text File  |  1993-09-23  |  4KB  |  191 lines

  1. //    Copyright 1993 Ralph Gonzalez
  2.  
  3. /*
  4. *    FILE:        trans.c
  5. *    AUTHOR:        R. Gonzalez
  6. *    CREATED:    October 3, 1990
  7. *
  8. *    methods for Transformation class and descendants.  Use to
  9. *    transform 3D coordinates.
  10. */
  11.  
  12. # include    "trans.h"
  13. # include    <math.h>
  14.  
  15. /******************************************************************
  16. *    initialize
  17. ******************************************************************/
  18. Transformation::Transformation(void)
  19. {
  20.     int        row,
  21.             col;
  22.             
  23.     for (row=0 ; row<4 ; row++)
  24.         for (col=0 ; col<4 ; col++)
  25.             if (row == col)
  26.                 m[row][col] = 1.;
  27.             else
  28.                 m[row][col] = 0.;
  29. }
  30.  
  31. /******************************************************************
  32. *    combines two transformations to produce a new one.  This is
  33. *    done by right-multiplying the first homogeneous transformation
  34. *    matrix with the next one.  That is, the transformations are
  35. *    sequenced from left to right.
  36. ******************************************************************/
  37. void    Transformation::combine(Transformation* first,
  38.                                 Transformation* next)
  39. {
  40.     int        row,
  41.             col;
  42.     
  43.     for (row=0 ; row<4 ; row++)
  44.         for (col=0 ; col<4 ; col++)
  45.             m[row][col] =    first->m[row][0] * next->m[0][col] +
  46.                             first->m[row][1] * next->m[1][col] +
  47.                             first->m[row][2] * next->m[2][col] +
  48.                             first->m[row][3] * next->m[3][col];
  49. }
  50.  
  51. /******************************************************************
  52. *    sets the transformation equal to the argument.
  53. ******************************************************************/
  54. void    Transformation::equate(Transformation* nnew)
  55. {
  56.     int        row,
  57.             col;
  58.     
  59.     for (row=0 ; row<4 ; row++)
  60.         for (col=0 ; col<4 ; col++)
  61.             m[row][col] = nnew->m[row][col];
  62. }
  63.  
  64. /******************************************************************
  65. *    set translation
  66. ******************************************************************/
  67. void    Translation::set(double tx,double ty,double tz)
  68. {
  69.     m[3][0] = tx;
  70.     m[3][1] = ty;
  71.     m[3][2] = tz;
  72. }
  73.  
  74. /******************************************************************
  75. *    set scaling
  76. ******************************************************************/
  77. void    Scaling::set(double sx,double sy,double sz)
  78. {
  79.     m[0][0] = sx;
  80.     m[1][1] = sy;
  81.     m[2][2] = sz;
  82. }
  83.  
  84. /******************************************************************
  85. *    set rotation about x
  86. ******************************************************************/
  87. void    Rotation_X::set(double theta)
  88. {
  89.     double    ct,
  90.             st;
  91.     
  92.     ct = cos(theta);
  93.     st = sin(theta);
  94.     
  95.     m[1][1] = ct;
  96.     m[1][2] = st;
  97.     m[2][1] = -st;
  98.     m[2][2] = ct;
  99. }
  100.  
  101. /******************************************************************
  102. *    set rotation about y
  103. ******************************************************************/
  104. void    Rotation_Y::set(double theta)
  105. {
  106.     double    ct,
  107.             st;
  108.     
  109.     ct = cos(theta);
  110.     st = sin(theta);
  111.     
  112.     m[0][0] = ct;
  113.     m[0][2] = -st;
  114.     m[2][0] = st;
  115.     m[2][2] = ct;
  116. }
  117.  
  118. /******************************************************************
  119. *    set rotation about z
  120. ******************************************************************/
  121. void    Rotation_Z::set(double theta)
  122. {
  123.     double    ct,
  124.             st;
  125.     
  126.     ct = cos(theta);
  127.     st = sin(theta);
  128.     
  129.     m[0][0] = ct;
  130.     m[0][1] = st;
  131.     m[1][0] = -st;
  132.     m[1][1] = ct;
  133. }
  134.  
  135. /******************************************************************
  136. *    set view transformation - converts from world coordinates to
  137. *    view (camera) coordinates.
  138. *
  139. *    This matrix is obtained by successively multiplying 5 basic tran-
  140. *    sformation matrices: (1) translation by (-x,-y,-z), (2) rotation
  141. *    about the world y axis by (-yaw), (3) rotation about the world x
  142. *    axis by (-pitch), (4) rotation about the world z axis by (-roll),
  143. *    and (5) negation of the x coordinate (to convert from right-handed
  144. *    world coordinates to a left-handed view coordinate system).
  145. *    Ref.: Hearn & Baker, "Computer Graphics", Prentice-Hall, 1986
  146. ******************************************************************/
  147. void    World_To_Camera::set(double x,double y,double z,
  148.                             double yaw,double pitch,double roll)
  149. {
  150.     double    cy,
  151.             cp,
  152.             cr,
  153.             sy,
  154.             sp,
  155.             sr,
  156.             k1,
  157.             k2;
  158.  
  159.     cy = cos(-yaw);
  160.     cp = cos(-pitch);
  161.     cr = cos(-roll);
  162.     sy = sin(-yaw);
  163.     sp = sin(-pitch);
  164.     sr = sin(-roll);
  165.     k1 = -x*cy-z*sy;
  166.     k2 = x*sy-z*cy;
  167.     
  168.     m[0][0] = -cy*cr+sy*sp*sr;
  169.     m[0][1] = cy*sr+sy*sp*cr;
  170.     m[0][2] = -sy*cp;
  171.     m[0][3] = 0.;
  172.     
  173.     m[1][0] = cp*sr;
  174.     m[1][1] = cp*cr;
  175.     m[1][2] = sp;
  176.     m[1][3] = 0.;
  177.     
  178.     m[2][0] = -sy*cr-cy*sp*sr;
  179.     m[2][1] = sy*sr-cy*sp*cr;
  180.     m[2][2] = cy*cp;
  181.     m[2][3] = 0.;
  182.     
  183.     m[3][0] = -k1*cr-y*cp*sr-k2*sp*sr;
  184.     m[3][1] = k1*sr-y*cp*cr-k2*sp*cr;
  185.     m[3][2] = -y*sp+k2*cp;
  186.     m[3][3] = 1.;
  187. }
  188.  
  189.  
  190.  
  191.